<?php
//部件异常记录

namespace app\admin\controller;

use app\admin\model\Component;
use app\admin\model\ProjectsOrder;
use app\admin\model\Projects;
use app\admin\model\ComponentExceptionInfoRe;
use think\facade\View;

class ComponentExceptionLogging extends Base
{
    protected $lists_fields = 'id,component_id,number,reason,status';  //数据表查询字段
    public function filter(&$map, &$field)
    {
        $field = $this->lists_fields;
        //列表显示各个具体部件的异常
        $component_id = input('component_id', 0, 'intval');
        $map = [
            ['component_id', '=', $component_id],
            ['status', '=', 1],
        ];
    }


    public function beforeDetail(&$field)
    {
        $field = $this->lists_fields;
    }

    public function attachedDetail(&$data)
    {
        $result = Component::where('id', '=', $data['component_id'])->where('status', '=', 1)->field('component_type,drawing_no,drawing_name,projects_order_id,pid')->find();

        //默认值
        if(empty($result))
        {
            $result = [
                'component_type' => 0,
                'drawing_no' => '',
                'drawing_name' => '',
                'projects_order_id' => 0,
                'pid' => 0,
            ];
        }else{
            $result = $result->toArray();
        }

        //类型
        $data['component_type'] = intval($result['component_type']);
        //部件图名
        $data['drawing_name'] = (string)$result['drawing_name'];
        //图号
        $data['drawing_no'] = (string)$result['drawing_no'];
        //所在订单
        $order_name = ProjectsOrder::where('id', '=', $result['projects_order_id'])->where('status', '=', 1)->field('proj_id,name')->find();

        //默认值
        if(empty($order_name))
        {
            $order_name['name'] = '';
            $order_name['proj_id'] = 0;
        }else{
            $order_name = $order_name->toArray();
        }

        $data['order_name'] = (string)$order_name['name'];
        //父级名称
        $parent_result = Component::where('status', '=', 1)->where('id', '=', $result['pid'])->field('component_type,drawing_no,drawing_name')->find();

        //默认值
        if(empty($parent_result))
        {
            $parent_result = [
                'component_type' => 0,
                'drawing_no' => '',
                'drawing_name' => ''
            ];
        }else{
            $parent_result = $parent_result->toArray();
        }

        $data['parent'] = [
            'component_type' => $parent_result['component_type'],
            'drawing_no' => $parent_result['drawing_no'],
            'drawing_name' => $parent_result['drawing_name'],
        ];
        //所在项目
        $project_name = Projects::where('status', '=', 1)->where('id', '=', $order_name['proj_id'])->value('title');
        $data['project_name'] = (string)$project_name;
        //子级数量
        $son_sum = Component::where('status', '=', 1)->where('pid', '=', $data['component_id'])->count();
        $data['son_sum'] = intval($son_sum);
    }

    /**
     * Desc: 处理异常
     * User: 苏鹏
     * Date: 2023/11/13
     * Time: 9:35
     * @return void
     */
    public function handlingException()
    {
        $id = input('id', 0, 'intval');
        $component_id = input('component_id', 0, 'intval');
        $reset_num = input('reset_num', 0, 'intval');
        $repair_num = input('repair_num', 0, 'intval');
        try{
            $comp_excep_info_re_m = new ComponentExceptionInfoRe();
            $comp_excep_info_re_m->startTrans();
            //将异常记录修改为已处理,记录重置或修复数量的信息
            $update_date = [
                'is_read'=>2,
                'content' => json_encode(['reset_num' => $reset_num, 'repair_num' => $repair_num])
            ];
            $comp_excep_info_re_m->where('id', '=', $id)->update($update_date);


            //通知计划部分，异常已提交需要确认生成订单(系统消息)
            $content = $comp_excep_info_re_m->getToPlanNoticeContent($id, $component_id, $reset_num, $repair_num);
            $department_ids = get_config_cache('department_ids');
            $plan_dep_id = $department_ids['plan_dep_id']; //计划部门
            $notice_data = [
                'tmp_flag' => 'system', //消息模板标识
                'to_dep_id' => $plan_dep_id, //发送的部门id
                'content' => [
                    'data' => $content
                ]
            ];
            event('SendNotice', $notice_data);
            $comp_excep_info_re_m->commit();
        }catch (\Exception $e){
            $comp_excep_info_re_m->rollback();
            return re_error_json($e->getMessage());
        }
        return re_success_json();

    }

    /**
     * Desc: 根据部件异常信息，生成生产信息
     * User: 苏鹏
     * Date: 2023/11/13
     * Time: 14:46
     * @return void
     */
    public function buyExceptionCreateComponentInfo()
    {
        $id = input('id', 0, 'intval');
        try{
            $comp_excep_info_re_m = new ComponentExceptionInfoRe();
            $comp_excep_info_re_m->startTrans();

            //生成生产计划
            $msg = $comp_excep_info_re_m->createProductionPlan($id);

            $comp_excep_info_re_m->commit();
        }catch (\Exception $e){
            $comp_excep_info_re_m->rollback();
            $msg = $e->getMessage();
        }

        return View::fetch('', ['msg' => $msg]);
    }
}